// pages/gamelib/index.js
const WXAPI = require('../../wxapi/main')
const colorUtil = require('../../utils/color')
const dateUtil = require('../../utils/date')
const app = getApp();
import Toast from 'tdesign-miniprogram/toast/index';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    gameList: [
    ],
    tag: {
      value: "-1",
      options: [
        { text: '全部', value: '-1' }, {
          value: '0',
          text: '新上架',
          icon: "/images/icon/hot.png"
        },
        { text: "合作", value: "合作" },
        { text: "多人", value: "多人" },
        { text: "角色扮演", value: "角色扮演" },
        { text: "回合战略", value: "回合战略" },
        { text: "动作", value: "动作" },
        { text: "开放世界", value: "开放世界" },
        { text: "剧情", value: "剧情" },
        { text: "Rogue", value: "Rogue" },
        { text: "格斗", value: "格斗" },
        { text: "射击", value: "射击" },
        { text: "冒险", value: "冒险" },
        { text: "体育", value: "体育" },
        { text: "模拟经营", value: "模拟经营" },
        { text: "休闲", value: "休闲" },
        { text: "第一人称", value: "第一人称" },
        { text: "策略", value: "策略" },
        { text: "对战", value: "对战" },
        { text: "解谜", value: "解谜" },
        { text: "卡牌", value: "卡牌" },
        { text: "节奏", value: "节奏" },
        { text: "建造", value: "建造" },
        { text: "Rouge", value: "Rouge" }
      ]

    },
    colorMap: {
      "Rogue": "#73BCF6",
      "Rouge": "#7680DD",
      "休闲": "#FBF605",
      "体育": "#B8AA45",
      "冒险": "#DF9C15",
      "剧情": "#5EED31",
      "动作": "#F83C70",
      "卡牌": "#B9FFAF",
      "合作": "#3289D2",
      "回合战略": "#6DDEC6",
      "多人": "#FD491C",
      "对战": "#A891CE",
      "射击": "#3BEDDA",
      "建造": "#A19A20",
      "开放世界": "#2E649B",
      "格斗": "#EF9A3B",
      "模拟经营": "#C16A42",
      "第一人称": "#5385E8",
      "策略": "#8A3273",
      "节奏": "#DDA91E",
      "角色扮演": "#A68C8E",
      "解谜": "#ABF6F3"
    },
    sorter: {
      text: "排序",
      value: "-1",
      options: [{
        value: '-1',
        text: '默认',
      },
      {
        value: 'score,2',
        text: '评分',
      },
      {
        value: 'release_time,2',
        text: '发售时间',
      }
      ]
    },
    themeColor: app.globalData.themeColor,
    gameType: 1,
    normalSearch: true,
    baseRefresh: {
      value: false,
    },
    loadingProps: {
      size: '50rpx',
    },
    backTopVisible: false,
    current_no: 0,
    page_size: 20,
    isLastPage: false,
    vipTip: false,
    vipTipTop: app.globalData.navBarHeight
  },
  closeNoticeNar() {
    this.setData({
      vipTip: false
    })
  },
  selectCondition(callback) {
    var param = {};
    var tag = this.data.tag.value;
    var sorter = this.data.sorter.value;
    var searchName = this.data.searchName;
    this.setData({
      isLastPage: false,
      vipTip: false
    })
    if (tag && tag != "-1") {
      this.setData({
        vipTip: tag == '0'
      })
      if (tag == '0')
        param.is_new = true;
      else
        param.tag = tag
    } else if (tag == "-1") {
      param.order_by = "score";
      param.order_type = "2";
    }

    if (sorter && sorter != "-1") {
      var index = sorter.indexOf(",");
      param.order_by = sorter.substring(0, index);
      param.order_type = sorter.substring(index + 1);
    }
    if (searchName && searchName != '')
      param.name = searchName
    param.type = this.data.gameType
    this.getGameList(param, callback);
  },


  onPullDownRefresh() {
    this.setData({
      current_no: 0,
      gameList: [],
    });
    this.selectCondition(() => {
      this.setData({
        'baseRefresh.value': false
      })
    });

  },
  onScroll(e) {
    const { scrollTop } = e.detail;

    this.setData({
      backTopVisible: scrollTop > 100,
    });
  },
  showHighSearch() {

    this.setData({
      normalSearch: false
    })
  },
  hideHighSearch() {

    this.setData({
      normalSearch: true
    })
  },
  toDetailsTap(e) {
    wx.navigateTo({
      url: "/pages/game-details/index?data=" + encodeURIComponent(JSON.stringify(e.currentTarget.dataset.data)) + "&colorMap=" + JSON.stringify(this.data.colorMap)
    })

  },
  search() {
    this.setData({
      current_no: 0,
      isLastPage: false,
      gameList: [],
    });
    this.selectCondition()
  },
  onTagChange(e) {
    var tag = this.data.tag;
    tag.value = e.detail;
    this.setData({
      tag,
      current_no: 0,
      isLastPage: false,
      gameList: [],
    });
    this.selectCondition()
  },
  onSorterChange(e) {
    var sorter = this.data.sorter;
    sorter.value = e.detail;
    this.setData({
      sorter,
      current_no: 0,
      isLastPage: false,
      gameList: [],
    });
    this.selectCondition()

  },
  onTabsClick(e) {
    var gameType = e.detail.name;
    this.setData({
      gameType,
      current_no: 0,
      page_size: 10,
      isLastPage: false,
      gameList: []
    });
    this.getGameList({ type: gameType });
  },

  getGameList(param, callback) {
    if (this.data.isLastPage) {
      return;
    }
    var { current_no, page_size, colorMap, l } = this.data;

    const realParam = Object.assign({}, param, { current_no, page_size });
    WXAPI.queryGame(realParam).then(res => {

      var gameList = res;

      if (gameList.length > 0) {
        gameList.forEach(item => {
          //处理显示颜色
          if (item.tag_list && item.tag_list.length > 0) {
            item.tag_list.forEach(tag => {
              if (colorMap[tag] == undefined)
                colorMap[tag] = colorUtil.getRandomColor()
            })
          }
          //处理时间
          item.release_time = dateUtil.toDateShort(item.release_time)

        })

        var newList = this.data.gameList.concat(gameList);
        this.setData({
          gameList: newList,
          colorMap,
        });
      } else {
        this.setData({
          isLastPage: true,
        });
      }
      if (callback) {
        callback()
        wx.hideNavigationBarLoading();

        return;
      }
      wx.hideNavigationBarLoading();
    }).catch((e) => {
      wx.hideNavigationBarLoading();
    });

  },
  loadMoreData() {
    if (this.data.isLastPage) {
      return;
    }
    var { current_no, page_size } = this.data;

    this.setData({
      current_no: current_no + page_size
    })
    this.selectCondition();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      current_no: 0,
      gameList: [],
    });
    this.selectCondition();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },





  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})